/*
 * @Author: Clark
 * @Email: haixuanwoTxh@gmail.com
 * @Date: 2023-01-04 15:52:16
 * @LastEditors: Clark
 * @LastEditTime: 2023-01-04 16:24:54
 * @Description: file content
 */

#include "apue.h"
#include <fcntl.h>

int main(int argc, char *argv[])
{
    int fd = -1;
    struct stat statbuf;
    struct timespec times[2];

    for (int i = 1; i < argc; i++)
    {
        if (stat(argv[i], &statbuf) < 0)
        {
            err_ret("%s: stat error", argv[i]);
            continue;
        }

        fd = open(argv[i], O_RDWR | O_TRUNC);
        if (fd < 0)
        {
            err_ret("%s: open error", argv[i]);
            continue;
        }

        // 文件大小截为0，文件时间保持
        times[0] = statbuf.st_atim;
        times[1] = statbuf.st_mtim;
        if (futimens(fd, times) < 0)
        {
            err_ret("%s: futimens error", argv[i]);
        }

        close(fd);
    }

    exit(0);
}
